﻿using Protocol;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Atom.Logic.Account;
using Atom.Logic.Fight;
using Atom.Logic.Forward;
using Atom.Logic.Match;
using Atom.Logic.Other;
using Atom.Logic.User;
using Atom.NetFrame;

namespace Atom
{
    /// <summary>
    /// 转发中心
    /// </summary>
    public class HandleCenter : AbsHandleCenter
    {
        private FightHandler m_FightHandler;
        private MatchHandle m_MatchHandle;
        private UserHandler m_UserHandler;
        private AccountHandle m_LoginHandle;
        private OtherHandler m_OtherHandler;
        private ForwardHandle m_ForwardHandler;

        public HandleCenter()
        {
            m_MatchHandle = new MatchHandle();
            m_UserHandler = new UserHandler();
            m_LoginHandle = new AccountHandle();
            m_FightHandler = new FightHandler();
            m_OtherHandler = new OtherHandler();
            m_ForwardHandler = new ForwardHandle();
        }

        public override void ClientClose(UserToken ut, string error)
        {
            //用户模块必须最后断开，因为，其他模块在断开时需要使用用户模块的数据
            //如果没有用户模块的数据，会导致其数据清除不干净
            Console.WriteLine("客户端断开,日志：" + error + "线程ID" + Thread.CurrentThread.ManagedThreadId);
            m_OtherHandler.ClientClose(ut, error);
            m_FightHandler.ClientClose(ut, error);
            m_MatchHandle.ClientClose(ut, error);
            m_LoginHandle.ClientClose(ut, error);
            m_UserHandler.ClientClose(ut, error);
            m_ForwardHandler.ClientClose(ut, error);
        }

        public override void ClientConnect(UserToken ut)
        {
            Console.WriteLine("客户链接,日志：" + "线程ID" + Thread.CurrentThread.ManagedThreadId);
        }

        public override void MessageReceive(UserToken ut, object message)
        {
            SocketModel sm = message as SocketModel;
            switch (sm.Type)
            {
                case GameProtocol.TYPE_ACCOUNT:
                    m_LoginHandle.MessageReceive(ut, sm);
                    break;
                case GameProtocol.TYPE_USER:
                    m_UserHandler.MessageReceive(ut, sm);
                    break;
                case GameProtocol.TYPE_MATCH:
                    m_MatchHandle.MessageReceive(ut, sm);
                    break;
                case GameProtocol.TYPE_FIGHT:
                    m_FightHandler.MessageReceive(ut, sm);
                    break;
                case GameProtocol.TYPE_OTHER:
                    m_OtherHandler.MessageReceive(ut, sm);
                    break;
                case GameProtocol.TYPE_FORWARD:
                    m_ForwardHandler.MessageReceive(ut, sm);
                    break;
            }
        }
    }
}